//
//  File.swift
//  剂查查
//
//  Created by shoubin tao on 2024/11/30.
//  Copyright © 2024 shipin. All rights reserved.
//

import CommonTools
import TYUIKit

import UIKit

public var WIDTH = UIScreen.main.bounds.width

public var HEIGHT = UIScreen.main.bounds.height

class JCCTools:NSObject {
    class func token() ->String {
        let token = UserDefaults.standard.value(forKey: "token") as? String ?? ""
        return token
    }
    class func clearToken()  {
        UserDefaults.standard.set(nil, forKey: "token")
        
    }
}
public extension UIColor {
    
    /// 通过16进制的字符串创建UIColor
    ///
    /// - Parameter hex: 16进制字符串，格式为#ececec
    convenience init (hex: String) {
        let hex = (hex as NSString).trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: hex)
        
        if hex.hasPrefix("#") {
            scanner.scanLocation = 1
        }
        
        var color: UInt32 = 0
        scanner.scanHexInt32(&color)
        
        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask
        
        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0
        self.init(red: red, green: green, blue: blue, alpha: 1)
    }
    
    
    /// 将UIColor转换为16进制字符串。
    func toHexString() -> String {
        let components = self.cgColor.components
        let r: CGFloat = components?[0] ?? 0.0
        let g: CGFloat = components?[1] ?? 0.0
        let b: CGFloat = components?[2] ?? 0.0
        
        let hexString = String(format: "#%02lX%02lX%02lX", lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255)))
        return hexString
    }
}

enum PvivacyType: String {
    case privacy = "pri"
    case user = "user"
    
    var title: String {
        self == .privacy ? "隐私协议" : "用户协议"
    }
}

func showPrivacy(type: PvivacyType) {
    let vc = JCCPrivacyViewController()
    vc.type = type
    TY_TopViewController().navigationController?.pushViewController(vc, animated: true)
}

func jcc_gradeColor(grade: String) -> UIColor? {
    var hex = ""
    switch grade {
    case "A":
        hex = "#00CC70"
    case "B":
        hex = "#0EAD66"
    case "B-":
        hex = "#8DA805"
    case "C":
        hex = "#FEC817"
    case "D":
        hex = "#E75049"
    default:
        hex = "#00CC70"
    }
    return UIColor(hex: hex)
}
